package com.common.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;

import com.common.util.Tools;
import com.manage.auth.MyInvocationSecurityMetadataSource;
import com.model.auth.User;

/**
 * 判断用户是否拥有按钮权限
 * @author zzx
 *
 */
public class ButtonTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	
	private String preUrl;//权限url
	private String href;//连接
	private String onclick;//点击事件
	private String name;//按钮名称
	public int doEndTag() throws JspException 
	{
		try {
			
			JspWriter out = pageContext.getOut();
			boolean bool = true;
			User user = Tools.getCurrentUser();
			if(StringUtils.isNotBlank(preUrl)){
				bool = MyInvocationSecurityMetadataSource.checkUrlAndRoleCode(preUrl,user.getRoleList());
				//System.out.println("bool="+bool);
			}
			
			if(!bool){
				out.print("");
				return EVAL_PAGE;
			}
			
			String html = "<a ";
			
			//String html = "<a class=\"button\"";
			
			if(StringUtils.isBlank(href)){
				href = "javascript:void(0)";
			}
			html += "href=\""+href+"\"";
			
			if(StringUtils.isNotBlank(onclick)){
				html += "onclick=\""+onclick+"\"";
			}
			html +=">";
			if(StringUtils.isBlank(name)){
				name = "按 钮";
			}
			html +=name +"</a>";
			out.print(html);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	/***** 使用例子
	  
	  web.xml 中使用
	  <jsp-config>
     <taglib>
       <taglib-uri>/WEB-INF/bottonTab</taglib-uri>
       <taglib-location>/WEB-INF/bottonTab.tld</taglib-location>
     </taglib>
   </jsp-config> 
   
      页面中引入标签
	 <%@taglib uri="/WEB-INF/bottonTab" prefix="myButton"%>
	 
	 页面中使用
	  <myButton:bottonTag name="创建用户" preUrl="/user/add.action" href="${ctx}/user/add.action"/>
	  
	 ****/
	public int doStartTag() throws JspException 
	{
		return SKIP_BODY;
	}


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPreUrl() {
		return preUrl;
	}

	public void setPreUrl(String preUrl) {
		this.preUrl = preUrl;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public String getOnclick() {
		return onclick;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}
}
